//
//  DEHomePictureView.m
//  DeDao
//
//  Created by PEIYANG on 16/9/25.
//  Copyright © 2016年 YP. All rights reserved.
//

#import "DEHomePictureView.h"

@implementation DEHomePictureView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _imageArray = @[@"collection",@"collection",@"collection",@"collection"];
        _number = 2;
        [self setupChildViewsWithNumberPerCol:self.number];
    }
    return self;
}

- (void)setupChildViewsWithNumberPerCol:(NSInteger)number{
    CGFloat btnW = (YPScreenW - (number + 1) * margin) / number;
    CGFloat btnH = btnW * 0.5;
    CGFloat btnX;
    CGFloat btnY;
    for (NSInteger index = 0; index<_imageArray.count; index++) {
        UIButton *btn = [[UIButton alloc]init];
        [btn setImage:[UIImage imageNamed:_imageArray[index]] forState:UIControlStateNormal];
        btn.tag = index;
        btn.imageView.layer.cornerRadius = 5;
        btn.imageView.layer.masksToBounds = YES;
        NSInteger hang = index / number;
        NSInteger lie = index % number;
        btnX = margin + (btnW + margin) * lie;
        btnY = margin + (btnH + margin) * hang;
        
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [self addSubview:btn];
        
        [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    _cellH = _imageArray.count / number * btnH + (_imageArray.count / number + 1) * margin;
    
}

- (void)BtnClick:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(homePictureView:btnClick:)]) {
        [self.delegate homePictureView:self btnClick:button];
    }
}

@end
